<?php

class ProductsModule extends fvModule {

    function __construct () {
        $moduleName = strtolower(substr(__CLASS__, 0, -6));

        parent::__construct(fvSite::$fvConfig->get("modules.{$moduleName}.smarty.template"), 
                            fvSite::$fvConfig->get("modules.{$moduleName}.smarty.compile"), 
                            fvSite::$Layoult);
    }

    function showIndex() {
    	$loggedUser = fvSite::$fvSession->getUser();
    	if ($loggedUser->is_root || $loggedUser->group_id == 1){
    		$params = array();
    	}
    	else{
    		$sections = array();
    		foreach ($loggedUser->categories_permitions as $cp){
    			$sections = array_unique(array_merge(SectionsManager::getInstance()->getSectionIDs($cp), $sections));
    		}
    		$query .= (($query)?" AND ":'') . "category_id in (!)";
    		$params[] = implode(',', $sections);
    		$query .= (($query)?" AND ":'') . "user_id = ?";
    		$params[] = $loggedUser->getPk();
    	}
    	$filter = fvSite::$fvSession->get(fvRequest::getInstance()->getRequestParameter("requestURL")."/filter");
    	$sort = fvRequest::getInstance()->getRequestParameter("sort") == 'asc' ? 'desc' : 'asc';
    	$type = in_array(fvRequest::getInstance()->getRequestParameter("type"), array('id', 'title', 'main_page', 'price', 'discount')) ? fvRequest::getInstance()->getRequestParameter("type") : 'id';
    	$this->__assign('sort', $sort);
    	//
        if (is_array($filter)) {
            if (!empty($filter['title'])) {
                $query .= (($query)?" AND ":'') . "title LIKE ?";
                $params[] = '%'.$filter['title'].'%';
                $this->__assign('filter_title', $filter['title']);
            }
        	if (!empty($filter['price_min'])) {
                $query .= (($query)?" AND ":'') . "price >= ?";
                $params[] = $filter['price_min'];
                $this->__assign('filter_price_min', $filter['price_min']);
            }
        	if (!empty($filter['price_max'])) {
                $query .= (($query)?" AND ":'') . "price <= ?";
                $params[] = $filter['price_max'];
                $this->__assign('filter_price_max', $filter['price_max']);
            }
        	if (!empty($filter['category_id'])) {
                $query .= (($query)?" AND ":'') . "category_id in (!)";
                $params_tmp[] = $filter['category_id'];
                $result[] = $filter['category_id'];
                do{
                	$Sections = SectionsManager::getInstance()->getAll("parent_id in (".implode(',', $params_tmp).")");
                	$params_tmp = array ();
                	if (count($Sections)){
	                	foreach ($Sections as $Section){
	                		$params_tmp[] = $Section->getPk();
	                		$result = array_unique(array_merge($params_tmp, $result));    		
	                	}
                	}
	                else{
	                	$stop = 0;
	                } 
                } while ($stop!=0);  
                $params[] = implode(',', $result);
                $this->__assign('filter_category', $filter['category_id']);
            }
        }
        $pager = new fvPager(ProductsManager::getInstance());
        $this->__assign('Products', $pager->paginate($query, "{$type} {$sort}", $params));
        $this->__assign('LoggedUser', $loggedUser);
        $SectionManager = SectionsManager::getInstance();
		$fvTree = new fvTree($SectionManager->getSectionTree());
        $this->__assign('Sections', $fvTree->return_tree());
        $sections_options[0] = 'основной пункт';
        $sections_options += $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
        $this->__assign('SectionsManager', $SectionManager);
        return $this->__display('products_list.tpl');    
    }

    function showEdit() {
    	$request = fvRequest::getInstance();
        if (!$Products = ProductsManager::getInstance()->getByPk($request->getRequestParameter('id'))) {
            $Products = new Products();
        }
        $SectionManager = SectionsManager::getInstance();
		$fvTree = new fvTree($SectionManager->getSectionTree());
        $this->__assign('Sections', $fvTree->return_tree());
        $sections_options = $fvTree->return_tree2();
        $this->__assign('SectionsOptions', $sections_options);
        if (SectionsManager::getInstance()->getByPk($Products->category_id)->filters){
	        $Filters = FilterManager::getInstance()->getAll("id in (".stripslashes(SectionsManager::getInstance()->getByPk($Products->category_id)->filters).")");
	        $query = "select * from krshop_product_filters where product_id=?"; //$Products->getPk()
	        $tmp = fvSite::$DB->getAll($query, array($Products->getPk()));
	        foreach ($tmp as $t){
	        	$ProductFilters[$t[filter_id]] = $t[val];
	        }
        }
        $this->__assign(array(
        	'set_filters' => $ProductFilters ? 0 : 1,
            'Products' => $Products,
        	'Filters' => $Filters,
        	'ProductFilters' =>$ProductFilters
        ));
        return $this->__display('products_edit.tpl');
    }
    
    function showUploadimage() {
        $uploader = new qqFileUploader();
		$result = $uploader->handleUpload(fvSite::$fvConfig->get('path.upload.tmp_dir'));
		//$result = $uploader->handleUpload('/home/klin/freelance/kramnyca_shop/krshop.uenota.org.ua/img/upload/tmp/');
		return htmlspecialchars(json_encode($result), ENT_NOQUOTES);
    }
}

?>
